package com.lovo.csc.config;

import com.lovo.util.MQUtil;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {
    //spring封装rabiitmq的代码 RabbitTemplate
    //注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }

    //采购标书交换机
    @Bean
    public DirectExchange tenderDirectExchange(){
        return new DirectExchange(MQUtil.tenderDirectExchange);
    }
    //采购标书队列
    @Bean
    public Queue tenderQueue(){
        return new Queue(MQUtil.tenderQueue);
    }

    //采购单交换机
    @Bean
    public DirectExchange PurchaseInfoDirectExchange(){
        return new DirectExchange(MQUtil.PurchaseInfoDirectExchange);
    }
    //采购单返回队列
    @Bean
    public Queue PurchaseInfoQueue(){
        return new Queue(MQUtil.PurchaseInfoQueue);
    }
    //采购单传输队列
    @Bean
    public Queue TrPurchaseInfoQueue(){
        return new Queue(MQUtil.TrPurchaseInfoQueue);
    }
    @Bean
    public Queue settlementQueue(){
        //关联死信交换机
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", MQUtil.directExchangeDead);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key",MQUtil.deadKey);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl",5000);
        return QueueBuilder.durable(MQUtil.settlementQueue).withArguments(args).build();
    }
    @Bean
    public DirectExchange directExchangeSettlement(){
        return new DirectExchange(MQUtil.directExchangeSettlement);
    }
     //绑定交换机和队列
    @Bean
    public Binding directExchangeOrderToOrderQueue(Queue settlementQueue,DirectExchange directExchangeSettlement){
     return BindingBuilder.bind(settlementQueue).to(directExchangeSettlement).with(MQUtil.settlementKey);
    }


    @Bean
    public Queue deadQueue(){
        return new Queue(MQUtil.deadQueue);
    }
    @Bean
    public DirectExchange directExchangeDead(){
        return new DirectExchange(MQUtil.directExchangeDead);
    }

    @Bean
    public Binding directExchangeDeadToDeadQueue(Queue deadQueue,DirectExchange directExchangeDead){
        return BindingBuilder.bind(deadQueue).to(directExchangeDead).with(MQUtil.deadKey);
    }
    //连接采购标书交换机和队列
    public Binding tenderDirectExchangeToTenderQueue(DirectExchange tenderDirectExchange,Queue tenderQueue){
        return BindingBuilder.bind(tenderQueue).to(tenderDirectExchange).with(MQUtil.tenderKey);
    }
    //连接采购单返回交换机和队列
    public Binding PurchaseInfoDirectExchangeToPurchaseInfoQueue(DirectExchange PurchaseInfoDirectExchange,Queue PurchaseInfoQueue){
        return BindingBuilder.bind(PurchaseInfoQueue).to(PurchaseInfoDirectExchange).with(MQUtil.PurchaseInfoKey);
    }
    //连接采购单传输交换机和队列
    public Binding PurchaseInfoDirectExchangeToTrPurchaseInfoQueue(DirectExchange PurchaseInfoDirectExchange,Queue TrPurchaseInfoQueue){
        return BindingBuilder.bind(TrPurchaseInfoQueue).to(PurchaseInfoDirectExchange).with(MQUtil.TrPurchaseInfoKey);
    }
    // 运存款成功的通知队列
    @Bean
    public Queue DepositQueue(){
        return new Queue(MQUtil.DepositQueue);
    }
    @Bean
    public DirectExchange PrepayExchange(){
        return new DirectExchange(MQUtil.PrepayExchange);
    }
    @Bean
    public Binding DepositQueueToDirectExchange(Queue DepositQueue,DirectExchange PrepayExchange){
        return BindingBuilder.bind(DepositQueue).to(PrepayExchange).with(MQUtil.depositKey);
    }

}
